﻿// My methods here.
//-----------------------------------------------------------------------
// <Copyright file=KhuyenMai company=DHPD>
// Copyright (c) DHPD Corporation (http://dhpd.edu.vn). All rights reserved.
// </copyright>
// <author>HP</author>
//-----------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KM.DAT;
using KM.ENT;
using KM.Models;

namespace KM.DAT
{
    /// <summary>
    /// This is datKhuyenMai class
    /// </summary>

    public class datKhuyenMai
    {
        public datKhuyenMai()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        SqlCommand cmd;
        SqlDataReader dr;
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        #region  === tạo mới và sửa KhuyenMai===
        public void createEdit(entKhuyenMai entKM)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiCreateEdit", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("KhuyenMaiId", entKM.KhuyenMaiId);
            cmd.Parameters.AddWithValue("TieuDe", entKM.TieuDe);
            cmd.Parameters.AddWithValue("GiaNiemYet", entKM.GiaNiemYet);
            cmd.Parameters.AddWithValue("GiaKM", entKM.GiaKM);
            cmd.Parameters.AddWithValue("ThoiGian", entKM.ThoiGian);
            cmd.Parameters.AddWithValue("Link", entKM.Link);
            cmd.Parameters.AddWithValue("ImageSmall", entKM.ImageSmall);
            cmd.Parameters.AddWithValue("SiteNguon", entKM.SiteNguon);
            cmd.Parameters.AddWithValue("Provider", entKM.Provider);
            cmd.Parameters.AddWithValue("Summary", entKM.Summary);
            cmd.Parameters.AddWithValue("ThuTu", entKM.ThuTu);
            cmd.Parameters.AddWithValue("TrangThai", entKM.TrangThai);
            cmd.Parameters.AddWithValue("NguoiTao", entKM.NguoiTao);
            cmd.Parameters.AddWithValue("NguoiSua", entKM.NguoiSua);

            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        public void createEdit(entKhuyenMai entKM, int LoaiKhuyenMaiId)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiCreate", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("KhuyenMaiId", entKM.KhuyenMaiId);
            cmd.Parameters.AddWithValue("TieuDe", entKM.TieuDe);
            cmd.Parameters.AddWithValue("GiaNiemYet", entKM.GiaNiemYet);
            cmd.Parameters.AddWithValue("GiaKM", entKM.GiaKM);
            cmd.Parameters.AddWithValue("ThoiGian", entKM.ThoiGian);
            cmd.Parameters.AddWithValue("Link", entKM.Link);
            cmd.Parameters.AddWithValue("ImageSmall", entKM.ImageSmall);
            cmd.Parameters.AddWithValue("SiteNguon", entKM.SiteNguon);
            cmd.Parameters.AddWithValue("Provider", entKM.Provider);
            cmd.Parameters.AddWithValue("Summary", entKM.Summary);
            cmd.Parameters.AddWithValue("ThuTu", entKM.ThuTu);
            cmd.Parameters.AddWithValue("TrangThai", entKM.TrangThai);
            cmd.Parameters.AddWithValue("NguoiTao", entKM.NguoiTao);
            cmd.Parameters.AddWithValue("NguoiSua", entKM.NguoiSua);
            cmd.Parameters.AddWithValue("LoaiKhuyenMaiId", LoaiKhuyenMaiId);
            cmd.Parameters.AddWithValue("webId", entKM.WebId);
            cmd.Parameters.AddWithValue("ThanhPho", entKM.ThanhPho);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion

        #region  === getall KhuyenMai===
        public List<entKhuyenMai> getAll(ref PagingInfoModel page)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiGetAll", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
            cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
            cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
            dr = cmd.ExecuteReader();
            entKhuyenMai entKM = new entKhuyenMai();
            List<entKhuyenMai> lst = new List<entKhuyenMai>();
            while (dr.Read())
            {
                entKM = new entKhuyenMai();
                entKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());
                entKM.TieuDe = dr["TieuDe"].ToString();
                if (dr["GiaNiemYet"].ToString() != "")
                {
                    entKM.GiaNiemYet = int.Parse(dr["GiaNiemYet"].ToString());
                }
                if (dr["GiaKM"].ToString() != "")
                {
                    entKM.GiaKM = int.Parse(dr["GiaKM"].ToString());
                }
                entKM.ThoiGian = dr["ThoiGian"].ToString();
                entKM.Link = dr["Link"].ToString();
                entKM.ImageSmall = dr["ImageSmall"].ToString();
                entKM.SiteNguon = dr["SiteNguon"].ToString();
                entKM.Provider = dr["Provider"].ToString();
                entKM.Summary = dr["Summary"].ToString();
                entKM.ThuTu = Convert.ToInt32(dr["ThuTu"].ToString());
                entKM.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());
                entKM.NguoiTao = Convert.ToInt32(dr["NguoiTao"].ToString());
                entKM.NgayTao = dr["NgayTao"].ToString();
                entKM.NguoiSua = Convert.ToInt32(dr["NguoiSua"].ToString());
                entKM.NgaySua = dr["NgaySua"].ToString();

                lst.Add(entKM);
            }

            dr.Close();
            if (lst.Count > 0)
            {
                // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
                page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            }

            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall KhuyenMai by my khuyen mai===
        public List<entKhuyenMai> getMyKhuyenMai(int mykhuyenmai)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiGetMyKhuyenMai", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@mykhuyenmai", mykhuyenmai);

            dr = cmd.ExecuteReader();
            entKhuyenMai entKM = new entKhuyenMai();
            List<entKhuyenMai> lst = new List<entKhuyenMai>();
            while (dr.Read())
            {
                entKM = new entKhuyenMai();
                entKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());
                entKM.TieuDe = dr["TieuDe"].ToString();
                if (dr["GiaNiemYet"].ToString() != "")
                {
                    entKM.GiaNiemYet = int.Parse(dr["GiaNiemYet"].ToString());
                }
                if (dr["GiaKM"].ToString() != "")
                {
                    entKM.GiaKM = int.Parse(dr["GiaKM"].ToString());
                }
                entKM.ThoiGian = dr["ThoiGian"].ToString();
                entKM.Link = dr["Link"].ToString();
                entKM.ImageSmall = dr["ImageSmall"].ToString();
                entKM.SiteNguon = dr["SiteNguon"].ToString();
                entKM.Provider = dr["Provider"].ToString();
                entKM.Summary = dr["Summary"].ToString();
                entKM.ThuTu = Convert.ToInt32(dr["ThuTu"].ToString());
                entKM.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());
                entKM.NguoiTao = Convert.ToInt32(dr["NguoiTao"].ToString());
                entKM.NgayTao = dr["NgayTao"].ToString();
                entKM.NguoiSua = Convert.ToInt32(dr["NguoiSua"].ToString());
                entKM.NgaySua = dr["NgaySua"].ToString();

                lst.Add(entKM);
            }

            dr.Close();
            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall KhuyenMaiHot===
        public List<entKhuyenMai> getKhuyenMaiHot(int thanhpho,ref PagingInfoModel page)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiHotGetAll", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
            cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
            cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
            cmd.Parameters.AddWithValue("@thanhpho", thanhpho);
            dr = cmd.ExecuteReader();
            entKhuyenMai entKM = new entKhuyenMai();
            List<entKhuyenMai> lst = new List<entKhuyenMai>();
            while (dr.Read())
            {
                entKM = new entKhuyenMai();
                entKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());
                entKM.TieuDe = dr["TieuDe"].ToString();
                if (dr["GiaNiemYet"].ToString() != "")
                {
                    entKM.GiaNiemYet = int.Parse(dr["GiaNiemYet"].ToString());
                }
                if (dr["GiaKM"].ToString() != "")
                {
                    entKM.GiaKM = int.Parse(dr["GiaKM"].ToString());
                }
                entKM.ThoiGian = dr["ThoiGian"].ToString();
                entKM.Link = dr["Link"].ToString();
                entKM.ImageSmall = dr["ImageSmall"].ToString();
                entKM.SiteNguon = dr["SiteNguon"].ToString();
                entKM.Provider = dr["Provider"].ToString();
                entKM.Summary = dr["Summary"].ToString();
                entKM.ThuTu = Convert.ToInt32(dr["ThuTu"].ToString());
                entKM.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());
                entKM.NguoiTao = Convert.ToInt32(dr["NguoiTao"].ToString());
                entKM.NgayTao = dr["NgayTao"].ToString();
                entKM.NguoiSua = Convert.ToInt32(dr["NguoiSua"].ToString());
                entKM.NgaySua = dr["NgaySua"].ToString();

                lst.Add(entKM);
            }

            dr.Close();
            if (lst.Count > 0)
            {
                // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
                page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            }

            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall KhuyenMai===
        public List<entKhuyenMai> getAllByForeignKey(int Id, byte type, ref PagingInfoModel page)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiGetAllByForeignKey", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("Id", Id);
            cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
            cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
            cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
            cmd.Parameters.AddWithValue("Type", type);
            cmd.Parameters.AddWithValue("mykhuyenmai", 0);
            dr = cmd.ExecuteReader();
            entKhuyenMai entKM = new entKhuyenMai();
            List<entKhuyenMai> lst = new List<entKhuyenMai>();
            while (dr.Read())
            {
                entKM = new entKhuyenMai();
                entKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());
                entKM.TieuDe = dr["TieuDe"].ToString();
                if (dr["GiaNiemYet"].ToString() != "")
                {
                    entKM.GiaNiemYet = int.Parse(dr["GiaNiemYet"].ToString());
                };
                if (dr["GiaKM"].ToString() != "")
                {
                    entKM.GiaKM = int.Parse(dr["GiaKM"].ToString());
                }
                entKM.ThoiGian = dr["ThoiGian"].ToString();
                entKM.Link = dr["Link"].ToString();
                entKM.ImageSmall = dr["ImageSmall"].ToString();
                entKM.SiteNguon = dr["SiteNguon"].ToString();
                entKM.Provider = dr["Provider"].ToString();
                entKM.Summary = dr["Summary"].ToString();
                entKM.ThuTu = Convert.ToInt32(dr["ThuTu"].ToString());
                entKM.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());
                entKM.NguoiTao = Convert.ToInt32(dr["NguoiTao"].ToString());
                entKM.NgayTao = dr["NgayTao"].ToString();
                entKM.NguoiSua = Convert.ToInt32(dr["NguoiSua"].ToString());
                entKM.NgaySua = dr["NgaySua"].ToString();

                lst.Add(entKM);
            }

            dr.Close();
            if (lst.Count > 0)
            {
                // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
                page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            }

            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall KhuyenMai by loaikhuyenmai===
        public List<entKhuyenMai> getAllByLoaiKhuyenMai(int Id, int thanhpho,int price, string q, ref PagingInfoModel page)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiGetAllByLoaiKhuyenMaiId", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("LoaiKhuyenMaiId", Id);
            cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
            cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
            cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
            cmd.Parameters.AddWithValue("@thanhpho", thanhpho);
            cmd.Parameters.AddWithValue("Price", price);
            cmd.Parameters.AddWithValue("q", q);
            dr = cmd.ExecuteReader();
            entKhuyenMai entKM = new entKhuyenMai();
            List<entKhuyenMai> lst = new List<entKhuyenMai>();
            while (dr.Read())
            {
                entKM = new entKhuyenMai();
                entKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());
                entKM.TieuDe = dr["TieuDe"].ToString();
                if (dr["GiaNiemYet"].ToString() != "") { entKM.GiaNiemYet = int.Parse(dr["GiaNiemYet"].ToString()); };
                if (dr["GiaKM"].ToString() != "")
                {
                    entKM.GiaKM = int.Parse(dr["GiaKM"].ToString());
                }
                entKM.ThoiGian = dr["ThoiGian"].ToString();
                entKM.Link = dr["Link"].ToString();
                entKM.ImageSmall = dr["ImageSmall"].ToString();
                entKM.SiteNguon = dr["SiteNguon"].ToString();
                entKM.Provider = dr["Provider"].ToString();
                entKM.Summary = dr["Summary"].ToString();
                entKM.ThuTu = Convert.ToInt32(dr["ThuTu"].ToString());
                entKM.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());
                entKM.NguoiTao = Convert.ToInt32(dr["NguoiTao"].ToString());
                entKM.NgayTao = dr["NgayTao"].ToString();
                entKM.NguoiSua = Convert.ToInt32(dr["NguoiSua"].ToString());
                entKM.NgaySua = dr["NgaySua"].ToString();

                lst.Add(entKM);
            }

            dr.Close();
            if (lst.Count > 0)
            {
                // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
                page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            }

            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall KhuyenMai===
        public List<entKhuyenMai> getAllByForeignKey(int Id, byte type, ref PagingInfoModel page, byte mykhuyenmai)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiGetAllByForeignKey", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("Id", Id);
            cmd.Parameters.AddWithValue("@PageIndex", page.CurrentPage);
            cmd.Parameters.AddWithValue("@PageSize", page.recordPerPage);
            cmd.Parameters.AddWithValue("@RecordCount", page.totalRecords).Direction = ParameterDirection.Output;
            cmd.Parameters.AddWithValue("Type", type);
            cmd.Parameters.AddWithValue("mykhuyenmai", mykhuyenmai);
            dr = cmd.ExecuteReader();
            entKhuyenMai entKM = new entKhuyenMai();
            List<entKhuyenMai> lst = new List<entKhuyenMai>();
            while (dr.Read())
            {
                entKM = new entKhuyenMai();
                entKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());
                entKM.TieuDe = dr["TieuDe"].ToString();
                if (dr["GiaNiemYet"].ToString() != "")
                {
                    entKM.GiaNiemYet = int.Parse(dr["GiaNiemYet"].ToString());
                };
                if (dr["GiaKM"].ToString() != "")
                {
                    entKM.GiaKM = int.Parse(dr["GiaKM"].ToString());
                }
                entKM.ThoiGian = dr["ThoiGian"].ToString();
                entKM.Link = dr["Link"].ToString();
                entKM.ImageSmall = dr["ImageSmall"].ToString();
                entKM.SiteNguon = dr["SiteNguon"].ToString();
                entKM.Provider = dr["Provider"].ToString();
                entKM.Summary = dr["Summary"].ToString();
                entKM.ThuTu = Convert.ToInt32(dr["ThuTu"].ToString());
                entKM.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());
                entKM.NguoiTao = Convert.ToInt32(dr["NguoiTao"].ToString());
                entKM.NgayTao = dr["NgayTao"].ToString();
                entKM.NguoiSua = Convert.ToInt32(dr["NguoiSua"].ToString());
                entKM.NgaySua = dr["NgaySua"].ToString();

                lst.Add(entKM);
            }

            dr.Close();
            if (lst.Count > 0)
            {
                // int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
                page.totalRecords = int.Parse(cmd.Parameters["@RecordCount"].Value.ToString());
            }

            sqlConnection.Close();
            return lst;
        }

        #endregion
        

        #region  === getinfo KhuyenMai===
        public entKhuyenMai getInfo(int KhuyenMaiId)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiGetInfo", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("KhuyenMaiId", DbType.Int32);
            sqla[0].Value = KhuyenMaiId;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entKhuyenMai entKM = new entKhuyenMai();
            if (dr.Read())
            {
                entKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());
                entKM.TieuDe = dr["TieuDe"].ToString();
                if (dr["GiaNiemYet"].ToString() != "") { entKM.GiaNiemYet = int.Parse(dr["GiaNiemYet"].ToString()); };
                if (dr["GiaKM"].ToString() != "")
                {
                    entKM.GiaKM = int.Parse(dr["GiaKM"].ToString());
                }
                entKM.ThoiGian = dr["ThoiGian"].ToString();
                entKM.Link = dr["Link"].ToString();
                entKM.ImageSmall = dr["ImageSmall"].ToString();
                entKM.SiteNguon = dr["SiteNguon"].ToString();
                entKM.Provider = dr["Provider"].ToString();
                entKM.Summary = dr["Summary"].ToString();
                entKM.ThuTu = Convert.ToInt32(dr["ThuTu"].ToString());
                entKM.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());
                entKM.NguoiTao = Convert.ToInt32(dr["NguoiTao"].ToString());
                entKM.NgayTao = dr["NgayTao"].ToString();
                entKM.NguoiSua = Convert.ToInt32(dr["NguoiSua"].ToString());
                entKM.NgaySua = dr["NgaySua"].ToString();
                entKM.WebId = int.Parse(dr["Webid"].ToString());

            }
            dr.Close();
            sqlConnection.Close();
            return entKM;
        }
        #endregion
        #region  === getinfo KhuyenMai===
        public entKhuyenMai checkLink(string link)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiCheckLink", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("link", DbType.Int32);
            sqla[0].Value = link;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entKhuyenMai entKM = new entKhuyenMai();
            if (dr.Read())
            {
                entKM.KhuyenMaiId = Convert.ToInt32(dr["KhuyenMaiId"].ToString());

            }
            dr.Close();
            sqlConnection.Close();
            return entKM;
        }
        #endregion

        #region === xoa KhuyenMai===
        public void delete(int id, byte type)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_KhuyenMaiDelete", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("KhuyenMaiId", DbType.Int32);
            sqla[0].Value = id;
            cmd.Parameters.AddWithValue("type", type);

            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion
    }
}
